package cn.yoyo.dao;

import cn.yoyo.entity.Content;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

@Repository
public class ContentDaoImpl implements ContentDao{
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Content findById(String contentId) {
        Query query = new Query(Criteria.where("id").is(contentId));
        return mongoTemplate.findOne(query,Content.class);
    }

    @Override
    public void update(Content content) {
        Update update = new Update();
        Query query = new Query(Criteria.where("id").is(content.getId()));
        update.set("content",content.getContent());
        mongoTemplate.updateFirst(query,update,Content.class);
    }

    @Override
    public Content add(Content content) {
        return mongoTemplate.insert(content);
    }

    @Override
    public void deleteById(String contentId) {
        Query query = new Query(Criteria.where("id").is(contentId));
        mongoTemplate.findAndRemove(query,Content.class);
    }
}
